<?php
/**
 * Created by PhpStorm.
 * User: jungshen
 * Date: 2018/8/2
 * Time: 14:45
 */

namespace app\api\controller\information;

use app\api\controller\BasicUserApi;
use Db;
use think\facade\Request;

class Article extends BasicUserApi
{
    /**
     * @Notes: 发布文章
     * @author: Forska
     * @email: 736523388@qq.com
     * @DateTime: 2018/10/17 11:08
     */
    public function push(){
        $title = Request::param('title');
        $type = Request::param('type');
        $childs = Request::param('childs');
        $content = Request::param('content');
        $images = Request::param('images');
        $province = Request::param('province');
        if(empty($title) || empty($type) || empty($content)){
            return json(['msg'=>'参数错误'],403);
        }
        $data = [
            'mid'=>UID,
            'title'=>$title,
            'type'=>$type,
            'childs'=>$childs,
            'images'=>$images,
            'province'=>$province,
            'content'=>$content,
            'create_at'=>time(),
            'status'=>'0'
        ];
        $re = Db::table('article')->insert($data);
        if($re){
            log_credit_change(UID,RELEASE_INFORMATION_CREDIT,RELEASE_INFORMATION,'发布文章加信用积分');
            return json(['msg'=>'操作成功'],200);
        }else{
            return json(['msg'=>'网络错误'],500);
        }

    }

    /**
     * @Notes: 删除文章
     * @author: Forska
     * @email: 736523388@qq.com
     * @DateTime: 2018/10/17 11:14
     */
    public function del(){
        $id = $_POST['id'];
        $map = [
            'mid'=>UID,
            'id'=>array('eq',$id)
        ];
        $info = Db::table('article')->where($map)->find();
        if(empty($info)){
            return json(['msg'=>'禁止访问，服务器收到请求，但是拒绝提供服务'],403);
        }
        $re = Db::table('article')->delete($id);
        if($re){
            return json(['msg'=>'操作成功'],200);
        }else{
            return json(['msg'=>'网络错误'],500);
        }
    }

    /**
     * @Notes: 我的文章列表
     * @return \think\response\Json
     * @author: Forska
     * @email: 736523388@qq.com
     * @DateTime: 2018/10/17 14:52
     */
    public function lists(){
        $list = Db::table('article')->where('mid',UID)->order('sort,create_at desc')->select();
        foreach ($list as &$item) {
            $item['create_at'] = date('Y-m-d',$item['create_at']);
            $item['images'] = explode('|',$item['images']);
        }
        return json(['msg'=>'success','data'=>$list],200);
    }

    /**
     * @Notes: 所有文章列表
     * @return \think\response\Json
     * @author: Forska
     * @email: 736523388@qq.com
     * @DateTime: 2018/10/24 16:34
     */
    public function alllist(){
        $page = Request::param('page',1);
        $pagesize = Request::param('pagesize',10);
        $title = Request::param('title');
        $db = Db::table('article')->where('status',1);
        if(!empty($title)){
            $db->where('title','like','%'.$title.'%');
        }
        $list = $db->page($page,$pagesize)->order('create_at desc')->select();
        foreach ($list as &$item) {
            $item['images'] = explode('|',$item['images']);
            $item['create_at'] = date('Y-m-d',$item['create_at']);
        }
        return json(['msg'=>'success','data'=>$list],200);
    }
    public function detail(){
        $id = Request::param('id');
        $map = [
            'id'=>array('eq',$id)
        ];
        $info = Db::table('article')->where($map)->find();
        if(empty($info) || $info['status'] != 1){
            return json(['msg'=>'找不到'],404);
        }
        Db::table('article')->where($map)->setInc('borwse',1);
        $info['create_at'] = date('Y-m-d',$info['create_at']);
        $info['images'] = explode('|',$info['images']);
        $info['zannum'] = Db::table('praise')->where('type',3)->where('attach_id',$info['id'])->count();
        return json(['msg'=>'success','data'=>$info],200);
    }
}